/**
 * @file
 * @auth jinguangguo
 * @date 2016/12/27
 */

import Vue from 'vue';
import filter from '../../../grsc-base/util/filter';
import commonService from '../../common/commonService';
import Loading from '../../../grsc-base/component/Loading.vue';
import WebBox from '../../component/WebBox/WebBox';
import CircleBall from '../../component/CircleBall.vue';
import Validator from '../../../grsc-base/component/Validator/main';
import {REG_EMAIL, REG_LOGPWD} from '../../../grsc-base/common/constants';
import lib from '../../common/lib';

import tpl from './tpl.html';
import service from './service';
lib.isGoMobilePage('m_reset.html');

export default Vue.extend({
    template: tpl,
    components: {
        Loading,
        Validator,
        WebBox,
        CircleBall
    },
    data() {
        return {
            form: {
                email: '',
                imgCode: '',
                identifyingCode: '',
                newPwd: '',
                confirmNewPwd: ''
            },
            rules: {
                email: {
                    tip: '',
                    required: true,
                    requiredTip: this.$t('common.emailRequireTip'),
                    pattern: REG_EMAIL,
                    patternTip: window.CODE.EMAIL[this.$i18n.locale]
                },
                imgCode: {
                    required: true,
                    requiredTip: this.$t('common.captchaRequireTip')
                },
                identifyingCode: {
                    required: true,
                    requiredTip: this.$t('common.verifyCodeRequireTip'),
                    pattern: '\\d{6}',
                    patternTip: window.CODE.IDENTIFY[this.$i18n.locale]
                },
                newPwd: {
                    required: true,
                    requiredTip: this.$t('common.loginPassRequireTip'),
                    minlength: 6,
                    minlengthTip: this.$t('common.ruleLoginPassLength'),
                    maxlength: 20,
                    maxlengthTip: this.$t('common.ruleLoginPassLength'),
                    pattern: REG_LOGPWD,
                    patternTip: this.$t('common.ruleLoginPassPattern')
                },
                confirmNewPwd: {
                    required:true,
                    requiredTip: this.$t('common.repeatPassRequireTip'),
                    equalTo: 'newPwd',
                    equalToTip: this.$t('common.passEqualTip')
                }
            },
            isLoading: true,
            imgCodeSrc: '',
            codeBtnText: this.$t('common.sendEmail'),
            codeLock:false,
            isIdentifyCaptcharOk: false,

            isRequesting: false,
            isSendingImgCode: false,    // 是否正在发送验证码

            randomKey: '',   // 发送图形验证码需要的动态时间参数
            sendingCode:false
        }
    },
    created() {
        this.changeImgCode();
    },
    mounted(){
        // ued体验优化
        setTimeout(()=>{
            this.isLoading = false;
        },500)
    },
    watch: {},
    methods: {

        /**
         * 验证登录密码
         */
        checkLoginPass() {
            this.rules.newPwd.tip = this.$t('common.ruleLoginPassLength');
        },

        checkLoginPassBlur() {
            this.rules.newPwd.tip = '';
        },

        /**
         * 改变图形验证码
         */
        changeImgCode() {
            this.randomKey = new Date().getTime();
            commonService.getCode({
                randomKey: this.randomKey
            }).done(rep => {
                this.imgCodeSrc = rep;
            });
        },

        /**
         * 短信验证码
         */
        sendCode(){
            this.rules.email.$check();

            if (this.form.imgCode === '') {
                this.rules.imgCode.$setError(this.$t('common.captchaRequireTip'));
            }

            if (this.rules.email.$valid === false || this.form.imgCode === '') {
                return;
            }

            let that = this;
            let loopTimeId = null;
            let count = 60;

            //登录后发送验证码不需要手机号信息
            this.sendingCode = true;
            commonService.sendCode({
                sendType: 'EMAIL',
                sendAccount: this.form.email
            }, {
                'captchar-code': this.form.imgCode,
                'captchar-no': this.randomKey
            }).done((rep) => {
                if (rep.code == window.CODE.SUCCESS) {
                    this.codeLock = true;//获取验证码锁定 true：正在获取验证码，不可点击   false：可以点击
                    this.rules.identifyingCode.$setRight(window.CODE.IDENTIFY_HAS_SEND[this.$i18n.locale]);
                    send();
                    loopTimeId = setInterval(function () {
                        send();
                    }, 1000)
                } else if (rep.code === window.CODE.EMAIL.CODE) {
                    this.changeImgCode();
                    this.rules.email.$setError(window.CODE.EMAIL[this.$i18n.locale]);
                } else if (rep.code === window.CODE.CAPTCHA.CODE) {
                    this.changeImgCode();
                    this.rules.imgCode.$setError(window.CODE.CAPTCHA[this.$i18n.locale]);
                } else if (rep.code === window.CODE.CAPTCHA_INVALID.CODE) {
                    this.rules.imgCode.$setError(window.CODE.CAPTCHA_INVALID[this.$i18n.locale]);
                } else if (rep.code === window.CODE.IDENTIFY_HAS_SEND.CODE) {
                    this.rules.identifyingCode.$setError(window.CODE.IDENTIFY_HAS_SEND[this.$i18n.locale]);
                }
            }).fail((rep) => {
                window.error(rep.msg);
            }).always(()=>{
                this.sendingCode = false;
            });

            function send() {
                if (count > 0) {
                    that.codeBtnText = count + that.$t('common.secondUnit');
                    count--;
                } else {
                    clearInterval(loopTimeId);
                    that.codeBtnText = that.$t('common.retryVerifyCode');
                    that.codeLock = false;
                }
            }

        },

        submit() {
            if (this.rules.$checkAll() === false) {
                return;
            }

            let param = {
                'loginPassword': this.form.newPwd
            };

            let header = {
                'account-no': this.form.email,
                'service-code': this.form.identifyingCode
            };

            this.isRequesting = true;
            service.resetLoginPwd(param, header).done(rep => {
                if (rep.code === window.CODE.SUCCESS) {
                    window.success(this.$t('common.success'), () => {
                        window.location.href = './index.html';
                    });
                } else if (rep.code === window.CODE.IDENTIFY.CODE) {
                    this.rules.identifyingCode.$setError(window.CODE.IDENTIFY[this.$i18n.locale]);
                } else if (rep.code === window.CODE.IDENTIFY_INVALID.CODE) {
                    this.rules.identifyingCode.$setError(window.CODE.IDENTIFY_INVALID[this.$i18n.locale]);
                } else if (rep.code === window.CODE.HAS_NO_REGISTER.CODE) {
                    this.rules.email.$setError(window.CODE.HAS_NO_REGISTER[this.$i18n.locale]);
                } else {
                    window.error(rep.msg);
                }
            }).always(rep => {
                this.isRequesting = false;
            });

        }

    }
});